package com.edu.internship.busi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.edu.internship.busi.dto.UserDTO;
import com.edu.internship.busi.entity.Role;
import com.edu.internship.busi.entity.UserRole;
import com.edu.internship.busi.service.IRoleService;
import com.edu.internship.busi.service.IUserRoleService;
import com.edu.internship.glabal.constants.ResultCodeEnum;
import com.edu.internship.glabal.exception.BusinessException;
import com.edu.internship.util.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.tools.ant.taskdefs.optional.extension.LibFileSet;
import org.checkerframework.checker.units.qual.A;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("api/userRole")
@Slf4j
@Api(tags = {"用户角色的关联控制器"})
public class UserRoleController {

    @Resource
    private IUserRoleService userRoleService;
    @Resource
    private IRoleService roleService;

    @PostMapping("/remove")
    @ApiOperation(value = "清除身份信息-userRole",notes = "清除身份信息-userRole")
    public boolean remove(@RequestBody @Validated UserDTO dto){
        LambdaQueryWrapper<UserRole> urQw = Wrappers.lambdaQuery();
        urQw.eq(StringUtils.isNotBlank(dto.getId()),UserRole::getUId,dto.getId());
        return userRoleService.remove(urQw);
    }

    @GetMapping("/create")
    @ApiOperation(value = "添加关联信息",notes = "添加关联信息")
    public boolean create(String uId,String roleDesc){
        LambdaQueryWrapper<Role> rQw = Wrappers.lambdaQuery();
        rQw.eq(StringUtils.isNotBlank(roleDesc),Role::getRoleDesc,roleDesc);
        UserRole userRole = new UserRole();
        userRole.setId(Tools.createRandomCode(5,true));
        userRole.setCreatedTime(LocalDateTime.now());
        userRole.setRId(roleService.list(rQw).get(0).getId());
        userRole.setUId(uId);
        userRoleService.save(userRole);
        return true;
    }

    @GetMapping("/getIdentity")
    @ApiOperation(value = "获取身份信息",notes = "获取身份信息")
    public List<String> getIdentity(String id){
        LambdaQueryWrapper<UserRole> urQw = Wrappers.lambdaQuery();
        urQw.eq(StringUtils.isNotBlank(id),UserRole::getUId,id);
        List<String> urList = userRoleService.list(urQw).stream().map(UserRole::getRId).collect(Collectors.toList());
        if (urList.size() == 0) {
            throw new BusinessException(ResultCodeEnum.DATABASE_ERROR,"该用户没有身份");
        }
        LambdaQueryWrapper<Role> rQw = Wrappers.lambdaQuery();
        rQw.in(Role::getId,urList);
        return roleService.list(rQw).stream().map(Role::getRoleDesc).collect(Collectors.toList());
    }
}
